package com.newworld.dmsapp2.activity.history;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.activity.serviceapply.ViewPagerActivity;
import com.newworld.dmsapp2.fragment.bean.HistortyDetail;
import com.newworld.dmsapp2.util.Config;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class TimelineAdapter extends BaseAdapter {

	private Context context;
	private List<HistortyDetail> list = new ArrayList<HistortyDetail>();
	private LayoutInflater inflater;
	private Intent intent;
	private String detail1, detail1_content, detail4, detail4_content, detail5,
			detail5_content;
	public static final DisplayImageOptions options = new DisplayImageOptions.Builder()
			.cacheInMemory(false) // 设置下载的图片是否缓存在内存中
			.cacheOnDisk(false).bitmapConfig(Bitmap.Config.RGB_565) // 设置下载的图片是否缓存在SD卡中
			.displayer(new RoundedBitmapDisplayer(10)) // 设置成圆角图片    
			.build();

	public TimelineAdapter(Context context, List<HistortyDetail> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {

		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(
					R.layout.activity_history_search_timeline_listitem, null);
			viewHolder = new ViewHolder();
			viewHolder.ly1 = (LinearLayout) convertView
					.findViewById(R.id.his_ly1);
			viewHolder.ly2 = (LinearLayout) convertView
					.findViewById(R.id.his_ly2);
			viewHolder.ly3 = (LinearLayout) convertView
					.findViewById(R.id.his_ly3);
			viewHolder.ly4 = (LinearLayout) convertView
					.findViewById(R.id.his_ly4);
			viewHolder.ly5 = (LinearLayout) convertView
					.findViewById(R.id.his_ly5);
			viewHolder.ly6 = (LinearLayout) convertView
					.findViewById(R.id.his_ly6);
			viewHolder.detail1 = (TextView) convertView
					.findViewById(R.id.his_detail1);
			viewHolder.detail2 = (TextView) convertView
					.findViewById(R.id.his_detail2);
			viewHolder.detail3 = (TextView) convertView
					.findViewById(R.id.his_detail3);
			viewHolder.detail4 = (TextView) convertView
					.findViewById(R.id.his_detail4);
			viewHolder.detail5 = (TextView) convertView
					.findViewById(R.id.his_detail5);
			viewHolder.detail6 = (TextView) convertView
					.findViewById(R.id.his_detail6);
			viewHolder.detail1_content = (TextView) convertView
					.findViewById(R.id.his_detail1_content);
			viewHolder.detail2_content = (TextView) convertView
					.findViewById(R.id.his_detail2_content);
			viewHolder.detail2_content2 = (TextView) convertView
					.findViewById(R.id.his_detail2_content2);
			viewHolder.detail4_content = (TextView) convertView
					.findViewById(R.id.his_detail4_content);
			viewHolder.detail5_content = (TextView) convertView
					.findViewById(R.id.his_detail5_content);
			viewHolder.detail3_rating = (RatingBar) convertView
					.findViewById(R.id.his_detail3_ratingbar);
			viewHolder.detail6_image1 = (ImageView) convertView
					.findViewById(R.id.his_detail6_two_photo1);
			viewHolder.detail6_image2 = (ImageView) convertView
					.findViewById(R.id.his_detail6_two_photo2);
			viewHolder.detail6_voice = (ImageView) convertView
					.findViewById(R.id.his_detail6_voice);
			viewHolder.detail6_voicetime = (TextView) convertView
					.findViewById(R.id.his_detail6_voicetime);
			viewHolder.detail2_content2.getPaint().setFlags(
					Paint.UNDERLINE_TEXT_FLAG); // 下划线
			viewHolder.detail2_content2.getPaint().setAntiAlias(true);// 抗锯齿
			viewHolder.line = (View) convertView
					.findViewById(R.id.his_detail_view);
			viewHolder.detail1.setTag(position);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
			viewHolder.detail1.setTag(position);
		}
		
		position = (Integer) viewHolder.detail1.getTag();
		if (null != list.get(position).getDetail1()) {
			detail1 = list.get(position).getDetail1().toString();
		}
		if (null != list.get(position).getDetail1_content()) {
			detail1_content = list.get(position).getDetail1_content()
					.toString();
		}

		if (null != list.get(position).getDetail2()) {
			viewHolder.detail2.setVisibility(View.VISIBLE);
			viewHolder.detail2_content.setVisibility(View.VISIBLE);

			String detail2 = list.get(position).getDetail2().toString();
			viewHolder.detail2.setText(detail2);
			if (null != list.get(position).getDetail2_content()) {
				viewHolder.detail2_content.setVisibility(View.VISIBLE);
				String detail2_content = list.get(position)
						.getDetail2_content().toString();
				viewHolder.detail2_content.setText(detail2_content);
			} else {
				viewHolder.detail2_content.setVisibility(View.GONE);
			}
			if (null != list.get(position).getDetail2_content2()) {
				viewHolder.detail2_content2.setVisibility(View.VISIBLE);
				String detail2_content2 = list.get(position)
						.getDetail2_content2().toString();
				viewHolder.detail2_content2.setText(detail2_content2);
			} else {
				viewHolder.detail2_content2.setVisibility(View.GONE);
			}
		} else {
			viewHolder.detail2.setVisibility(View.GONE);
			viewHolder.detail2_content.setVisibility(View.GONE);
		}

		viewHolder.detail1.setText(detail1);
		viewHolder.detail1_content.setText(detail1_content);

		if (viewHolder.detail1.getText().toString().contains("报修时间")) {// 报修时间
			viewHolder.line.setVisibility(View.INVISIBLE);
			viewHolder.ly3.setVisibility(View.GONE);
			viewHolder.ly4.setVisibility(View.GONE);
			viewHolder.ly5.setVisibility(View.GONE);
			viewHolder.ly6.setVisibility(View.VISIBLE);
			if (null != list.get(position).getDetail6_image1()
					&& !list.get(position).getDetail6_image1().equals("")) {
				//list.get(position).getDetail6_image1() = list.get(position).getDetail6_image1();
				viewHolder.detail6_image1.setVisibility(View.VISIBLE);
				ImageLoader.getInstance().displayImage(
						"file://" + Config.IMAGE_PATH + list.get(position).getDetail6_image1(),
						viewHolder.detail6_image1, options);
			} else {
				viewHolder.detail6_image1.setVisibility(View.INVISIBLE);
			}
			if (null != list.get(position).getDetail6_image2()
					&& !list.get(position).getDetail6_image2().equals("")) {
				//list.get(position).getDetail6_image2() = list.get(position).getDetail6_image2();
				viewHolder.detail6_image2.setVisibility(View.VISIBLE);
				ImageLoader.getInstance().displayImage(
						"file://" + Config.IMAGE_PATH + list.get(position).getDetail6_image2(),
						viewHolder.detail6_image2, options);
			} else {
				viewHolder.detail6_image2.setVisibility(View.INVISIBLE);
			}
			if (null != list.get(position).getDetail6_voice()
					&& !list.get(position).getDetail6_voice().equals("")) {
				//list.get(position).getDetail6_voice() = list.get(position).getDetail6_voice();
				viewHolder.detail6_voice.setVisibility(View.VISIBLE);
				viewHolder.detail6_voicetime.setVisibility(View.VISIBLE);
				viewHolder.detail6_voicetime.setText(list.get(position).getDetail6_voicetime());
			} else {
				viewHolder.detail6_voice.setVisibility(View.INVISIBLE);
				viewHolder.detail6_voicetime.setVisibility(View.INVISIBLE);
			}
		/*	if (null != list.get(position).getDetail6_voicetime()
					&& !list.get(position).getDetail6_voicetime().equals("")) {
				//list.get(position).getDetail6_voice() = list.get(position).getDetail6_voice();
				viewHolder.detail6_voicetime.setVisibility(View.VISIBLE);
			} else {
				viewHolder.detail6_voicetime.setVisibility(View.INVISIBLE);
			}*/

		} else if (viewHolder.detail1.getText().toString().contains("完成时间")) {// 完成时间
			viewHolder.line.setVisibility(View.VISIBLE);
			viewHolder.ly1.setVisibility(View.VISIBLE);
			viewHolder.ly2.setVisibility(View.VISIBLE);
			viewHolder.ly3.setVisibility(View.VISIBLE);
			viewHolder.ly4.setVisibility(View.VISIBLE);
			viewHolder.ly5.setVisibility(View.VISIBLE);
			viewHolder.ly6.setVisibility(View.VISIBLE);
			if (null != list.get(position).getDetail4()) {
				detail4 = list.get(position).getDetail4().toString();
			}
			if (null != list.get(position).getDetail4_content()) {
				detail4_content = list.get(position).getDetail4_content()
						.toString();
			}
			if (null != list.get(position).getDetail5()) {
				detail5 = list.get(position).getDetail5().toString();
			}
			if (null != list.get(position).getDetail5_content()) {
				detail5_content = list.get(position).getDetail5_content()
						.toString();
			}

			viewHolder.detail3_rating.setRating(Integer.parseInt(list.get(
					position).getDetail3_content()));
			viewHolder.detail4.setText(detail4);
			viewHolder.detail4_content.setText(detail4_content);
			viewHolder.detail5.setText(detail5);
			viewHolder.detail5_content.setText(detail5_content);
			if (null != list.get(position).getDetail6_image1()
					&& !list.get(position).getDetail6_image1().equals("")) {
				viewHolder.detail6_image1.setVisibility(View.VISIBLE);
				ImageLoader.getInstance().displayImage(
						"file://" + Config.IMAGE_PATH + list.get(position).getDetail6_image1(),
						viewHolder.detail6_image1, options);
			} else {
				viewHolder.detail6_image1.setVisibility(View.INVISIBLE);
			}
			if (null != list.get(position).getDetail6_image2()
					&& !list.get(position).getDetail6_image2().equals("")) {
				viewHolder.detail6_image2.setVisibility(View.VISIBLE);
				ImageLoader.getInstance().displayImage(
						"file://" + Config.IMAGE_PATH + list.get(position).getDetail6_image2(),
						viewHolder.detail6_image2, options);
			} else {
				viewHolder.detail6_image2.setVisibility(View.INVISIBLE);
			}
			if (null != list.get(position).getDetail6_voice()
					&& !list.get(position).getDetail6_voice().equals("")) {
				viewHolder.detail6_voice.setVisibility(View.VISIBLE);
				viewHolder.detail6_voicetime.setVisibility(View.VISIBLE);
				viewHolder.detail6_voicetime.setText(list.get(position).getDetail6_voicetime());
			} else {
				viewHolder.detail6_voice.setVisibility(View.INVISIBLE);
				viewHolder.detail6_voicetime.setVisibility(View.INVISIBLE);
			}
			/*if (null != list.get(position).getDetail6_voicetime()
					&& !list.get(position).getDetail6_voicetime().equals("")) {
				viewHolder.detail6_voicetime.setVisibility(View.VISIBLE);
			} else {
				viewHolder.detail6_voicetime.setVisibility(View.INVISIBLE);
			}*/

		} else if (viewHolder.detail1.getText().toString().contains("受理时间")) {// 受理时间
			viewHolder.ly2.setVisibility(View.GONE);
			viewHolder.ly3.setVisibility(View.GONE);
			viewHolder.ly4.setVisibility(View.GONE);
			viewHolder.ly5.setVisibility(View.GONE);
			viewHolder.ly6.setVisibility(View.GONE);
			viewHolder.line.setVisibility(View.VISIBLE);

		}else if (viewHolder.detail1.getText().toString().contains("终止时间")) {// 终止回访时间
			viewHolder.ly3.setVisibility(View.GONE);
			viewHolder.ly4.setVisibility(View.GONE);
			viewHolder.ly5.setVisibility(View.VISIBLE);
			viewHolder.detail5.setText("终止描述");
			//viewHolder.detail5_content.setText(detail5_content);
			viewHolder.detail5_content.setText(list.get(position).getDetail5_content());
			viewHolder.detail5_content.setVisibility(View.VISIBLE);
			viewHolder.ly6.setVisibility(View.GONE);
			viewHolder.line.setVisibility(View.VISIBLE);

		} else {
			viewHolder.ly3.setVisibility(View.GONE);
			viewHolder.ly4.setVisibility(View.GONE);
			viewHolder.ly5.setVisibility(View.GONE);
			viewHolder.ly6.setVisibility(View.GONE);
			viewHolder.line.setVisibility(View.VISIBLE);
		}
		viewHolder.detail6_image1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				int position = (Integer) viewHolder.detail1.getTag();
				Intent a = new Intent(context, ViewPagerActivity.class);
				a.putExtra("image1", "file://" + Config.IMAGE_PATH + list.get(position).getDetail6_image1());
				if(null != list.get(position).getDetail6_image2()){
					if (!list.get(position).getDetail6_image2().equals("")) {
						a.putExtra("image2", "file://" + Config.IMAGE_PATH
								+ list.get(position).getDetail6_image2());
					}
				}
				
				context.startActivity(a);
			}
		});
		viewHolder.detail6_image2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				int position = (Integer) viewHolder.detail1.getTag();
				Intent b = new Intent(context, ViewPagerActivity.class);
				b.putExtra("image1", "file://" + Config.IMAGE_PATH + list.get(position).getDetail6_image2());
				b.putExtra("image2", "file://" + Config.IMAGE_PATH + list.get(position).getDetail6_image1());
				context.startActivity(b);
			}
		});

		viewHolder.detail6_voice.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				int position = (Integer) viewHolder.detail1.getTag();
				File clickfile0 = new File(Config.VOICE_PATH + list.get(position).getDetail6_voice());
				Intent a = new Intent();
				a.setAction(Intent.ACTION_VIEW);

				a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				a.putExtra("oneshot", 0);
				a.putExtra("configchange", 0);

				a.setDataAndType(Uri.parse("file:" + clickfile0.getPath()),
						"audio/*");
				context.startActivity(a);
			}
		});

		viewHolder.detail2_content2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				int position = (Integer) viewHolder.detail1.getTag();
				intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
						+ list.get(position).getDetail2_content2()));
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	static class ViewHolder {
		public LinearLayout ly1, ly2, ly3, ly4, ly5, ly6;
		public TextView detail1, detail2, detail3, detail4, detail5, detail6;
		public TextView detail1_content;
		public TextView detail2_content;
		public TextView detail2_content2;
		public TextView detail4_content;
		public TextView detail5_content;
		public RatingBar detail3_rating;
		public ImageView detail6_image1, detail6_image2, detail6_voice;
		public TextView detail6_voicetime;
		public View line;

	}

}
